package com.example.qxfw.qxxx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.yj.EmergencyPoint;
import com.example.qxfw.common.entity.yj.EmergencyStandard;
import com.example.qxfw.common.entity.yj.WarnSignalInfo;
import com.example.qxfw.mongodb.qx.entity.QxImageFile;
import com.example.qxfw.mongodb.qx.service.QxImageFileService;
import com.example.qxfw.qxxx.service.EmergencyStandardService;
import com.example.qxfw.qxxx.service.WarnSignalInfoService;
import com.example.qxfw.qxxx.vo.WarnSignalInfoVo;
import com.example.qxfw.yjqx.service.YjfwService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.dmgis.weblib.MapCommon.dmGisStringToArray;

@Slf4j
@Api(tags = "气象信息-预警信号")
@RestController
@CrossOrigin
@RequestMapping("/WarningSignalController")
public class WarningSignalController {

    @Autowired
    private EmergencyStandardService emergencyStandardService;

    @Autowired
    private WarnSignalInfoService warnSignalInfoService;

    @Autowired
    private QxImageFileService qxImageFileService;
    @Autowired
    private YjfwService yjfwService;

    @ApiOperation("气象信息-预警信号-获取预警等级")
    @GetMapping("/getEmergencyStandard")
    public SysResult getEmergencyStandard() {
        List<EmergencyStandard> list = emergencyStandardService.getList();
        return SysResult.success(list);
    }

    @ApiOperation("气象信息-预警信号-根据开始时间、结束时间查询预警信号")
    @GetMapping("/getWarnSignalInfo")
    public SysResult getWarnSignalInfo(String city, String pdtStart, String pdtEnd) {
        if (city != null && pdtStart != null && pdtEnd != null){
            List<WarnSignalInfo> warnSignalInfos = warnSignalInfoService.getList(city, pdtStart, pdtEnd);
            List<WarnSignalInfoVo> warnSingInfo = new ArrayList<>();
            warnSignalInfos.forEach( i ->{
                WarnSignalInfoVo warnSignalInfoVo = new WarnSignalInfoVo();
                warnSignalInfoVo.setId(i.getId());
                warnSignalInfoVo.setArea(i.getArea());
                warnSignalInfoVo.setType(i.getType());
                warnSignalInfoVo.setColor(i.getColor());
                warnSignalInfoVo.setAnnouncedtime(i.getAnnouncedtime());
                warnSignalInfoVo.setJoinid(i.getJoinid());
                if (i.getPos()!=null){
                warnSignalInfoVo.setPos(dmGisStringToArray(i.getPos()));}
                warnSingInfo.add(warnSignalInfoVo);
            });
            if (warnSingInfo.size()>0){
                return SysResult.success(warnSingInfo);
            }
            return SysResult.fail("未查询到数据");
        }
        return SysResult.fail("请选择查询条件");
    }
    @ApiOperation("气象信息-预警信号-得到城市坐标")
    @GetMapping("/getAreaCity")
    public SysResult getAreaCity(String area) {
        List<EmergencyPoint> emergencyPoints = yjfwService.listPointByName(area);
        return SysResult.success(emergencyPoints);
    }

    @ApiOperation("气象信息-预警信号-得到城市列表")
    @GetMapping("/getWarnCity")
    public SysResult getWarnCityInfo() {
        List<String> warnSignalInfos = warnSignalInfoService.getCity();
        return SysResult.success(warnSignalInfos);
    }

    @ApiOperation("气象信息-预警信号-选中某一条，查找详细信息")
    @GetMapping("/getWarnSignalArea")
    public SysResult getWarnSignalArea(String stationName, String time) {
        List<String> area = warnSignalInfoService.getArea(stationName, time);
        return SysResult.success(area);
    }

    @ApiOperation("气象信息-预警信号-选中某一条，查找图片")
    @GetMapping("/getImage")
    public ResponseEntity<byte[]> getImage(String id) {
        System.err.println(id);
        QxImageFile qxImageFile = qxImageFileService.selectById(id);
        byte[] data = qxImageFile.getData();
        String name = qxImageFile.getName();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG); // 设置为图片的jpg类型
        headers.setContentDisposition(ContentDisposition.builder("attachment").filename(name + ".jpg").build());
        return new ResponseEntity<>(data, headers, HttpStatus.OK);
    }


}
